<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@page isELIgnored="false" %>
<%@ include file="/WEB-INF/views/common/includes.jsp" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>

<script type="text/javascript" src="<c:url value='/static/dataTables/media/js/jquery.dataTables.js'/>"></script>
<script type="text/javascript" src="<c:url value='/static/js/jquery.switchify-1.0.js'/>"></script>

<script type="text/javascript">
$(document).ready(function() {
	$('#example').dataTable( {
		"bPaginate": true,
		"bLengthChange": false,
		"sPaginationType": "full_numbers",
		"bProcessing": true,
		"bServerSide": true,
		"bFilter": true,
		"sAjaxSource": "<c:url value="/es/admin/user/list/table"/>",
		"sAjaxDataProp": "rows",
		"aoColumns": [
			  			{ "mDataProp": "id" },
			  			{ "mDataProp": "name" },
			  			{ "mDataProp": "email" },
			  			{ "mDataProp": "organizationId", "fnRender" : renderOrganizationNameById },
			  			{ "sName": "actions", "fnRender" : make_crud_actions, "bSortable": false, "bSearchable": false },
			  			{ "sName": "moreActions", "fnRender" : make_custom_actions, "bSortable": false, "bSearchable": false },
		  			],
		//"aaSorting": [[ 0, "desc" ]],
	});
});

function renderOrganizationNameById(oObj) {
	var organization = oObj.aData['organizationId']
	var a = "";
	$.ajax({
		async : false,
		type: 'POST',
		url: '<c:url value="organization/description"/>',    			
		data: {organizationId : organization},		
		dataType: 'json',
		success: function (organizationName) {
			a = organizationName;
		},
	});
	return a;

}

function make_crud_actions(oObj) {
	var id = oObj.aData['id'];
	
	var detailAction = '<a href="<c:url value="/${rc.locale.language}/admin/user/detail/"/>' + id + '">Detail</a>';
	var editAction = '<a href="<c:url value="/${rc.locale.language}/admin/user/edit/"/>' + id + '">Edit</a>';
	var deleteAction = '<a href="<c:url value="/${rc.locale.language}/admin/user/delete/"/>' + id + '">Delete</a>';
	
	return detailAction + "&nbsp;&nbsp;" + editAction + "&nbsp;&nbsp;" + deleteAction ; 
}

function make_custom_actions(oObj) {
	var id = oObj.aData['id'];
	
	var addApplications = '<a href="<c:url value="/${rc.locale.language}/admin/user/"/>' + id + '/applications">Add applications</a>';

	return addApplications; 
}
</script>

<div class="span-16 last table">		
	<table id="example">
		<thead>
			<tr>
				<th>Id</th>
				<th>Name</th>
				<th>Email</th>
				<th>Organization</th>
				<th>Actions</th>
				<th>More actions</th>
			</tr>
		</thead>
	</table>
</div>
<div class="span-16 last table_actions">			
  <a href='<c:url value='/${rc.locale.language}/admin/user/'/>' title='<fmt:message key="action.user.add" />'><fmt:message key="action.user.add"/></a>
</div>